﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.Framework;
using System.IO;
using CNVP.JShop.UI;
using CNVP.Framework.Utils;

namespace CNVP.JShop.Web.Admin
{
    public partial class BranksEdit : AdminPage
    {
        BLL.CNVP_Branks bll = new BLL.CNVP_Branks();
        private static Model.CNVP_Branks model = new Model.CNVP_Branks();
        public int id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!BLL.BLLCheckAdmin.LoginOk(Page, 74)) return;//判断页面权限
            if (!String.IsNullOrEmpty(Request.QueryString["ID"]) && Public.IsNumber(Convert.ToString(Request.QueryString["ID"])))
            {
                id = Convert.ToInt32(Request.QueryString["ID"]);
            }
            if (!IsPostBack)
            {
                if (id > 0)
                {
                    lbTitle.Text = " > 修改";
                    DataInfo(id);
                }
                else
                {
                    lbTitle.Text = " > 添加";
                }
            }

           
        }
        public void DataInfo(int id)
        {
            model = bll.GetSingle_CNVP_BranksById(id);
            if (model != null)
            {
                txtBrankName.Text = model.BrankName;
                txtBrankAddress.Text = model.BrankAddress;
                if (model.BrankPic != "")
                {
                    imgPic.ImageUrl =Config.UIConfig.InstallDir+model.BrankPic;
                    imgPic.Visible = true;
                }
                txtOrderID.Text = model.OrderID.ToString();
                txtBrankOtherName.Text = model.BrankOtherName;
                txtBrankDetail.Value = model.BrankDetail;
            }
        }

        protected void btUp_Click(object sender, EventArgs e)
        {
            String picName=String.Empty;
            String webPic = txtWebUrl.Text;
            String isWebUrl =Convert.ToString(Request.Form["isWebUrl"]);
            fileUp.Save();
            picName = fileUp.FullPath;
            if (id > 0)
            {
                #region 修改
                if (String.IsNullOrEmpty(picName) && webPic.ToLower()==@"http://")
                {
                    picName = model.BrankPic;
                }
                else
                {
                    if (!String.IsNullOrEmpty(isWebUrl))
                    {
                        picName = webPic;
                    }
                    else
                    {
                        picName = Config.UIConfig.InstallDir + picName;
                    }
                }
                int rows = new BLL.CNVP_Branks().Update_CNVP_Branks(
                       new Model.CNVP_Branks()
                       {
                           ID = id,
                           BrankName = txtBrankName.Text.Trim(),
                           BrankAddress = txtBrankAddress.Text,
                           BrankPic = picName,
                           OrderID = int.Parse(txtOrderID.Text.Trim()),
                           BrankOtherName = txtBrankOtherName.Text.Trim(),
                           BrankDetail = txtBrankDetail.Value
                       }

               );
                if (rows == 1)
                {
                    BasePage.MessageBox("修改成功！", "BranksList.aspx?Page=" + Request.QueryString["Page"] + "");
                }
                else
                { BasePage.MessageBox("修改失败！"); }
                #endregion
            }
            else
            {
                #region 添加
                if (!String.IsNullOrEmpty(isWebUrl))
                {
                    picName = webPic;
                }
                else
                {
                    picName = Config.UIConfig.InstallDir + picName;
                }
                int rows = new BLL.CNVP_Branks().Add_CNVP_Branks(
                    new Model.CNVP_Branks()
                    {
                        BrankName = txtBrankName.Text.Trim(),
                        BrankAddress = txtBrankAddress.Text,
                        BrankPic = picName,
                        OrderID = int.Parse(txtOrderID.Text.Trim()),
                        BrankOtherName = txtBrankOtherName.Text.Trim(),
                        IsDetele = 0,
                        BrankDetail = txtBrankDetail.Value
                    }
                );
                if (rows == 1)
                {
                    BasePage.MessageBox("添加成功！", "BranksList.aspx");
                }
                else
                { BasePage.MessageBox("添加失败！"); }
                #endregion
            }

        }
    }
}